﻿/**************************************************************************
创建日期:	2019/7/21 23:13:40     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)    ZhCun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.WebCommon.DBModels;
using System.Linq;
using ZhCun.DbCore.Cores;
using ZhCun.WebCommon.ViewModels;

namespace ZhCun.WebCommon.AL
{
    public abstract class ALHomeBase : BaseAL
    {
        /// <summary>
        /// 1：用户菜单，2：系统菜单，3：所有菜单
        /// </summary>
        protected abstract int MenuType { get; }

        /// <summary>
        /// 获取主页实体对象，菜单列表，
        /// </summary>
        public VM_HomeIndex GetHomeModel()
        {
            VM_HomeIndex vm = new VM_HomeIndex();
            vm.UserName = LoginUser.UserName;
            vm.Menus = GetViewMenu();
            return vm;
        }

        /// <summary>
        /// 得到视图菜单
        /// </summary>
        protected virtual List<VM_HomeMenu> GetViewMenu()
        {
            var sqlWhere = DB.CreateSqlBuilder();
            sqlWhere.AddSQLText("IsEnable = 1 and IsApi = 0");
            if (MenuType == 1)
            {
                sqlWhere.AddSQLText(" and IsSystem = 0");
            }
            else if (MenuType == 2)
            {
                sqlWhere.AddSQLText(" and IsSystem = 1");
            }
            if (!IsManageUser())
            {                
                sqlWhere.AddSQLText(" and Id in (select MenuId from TSysRoleMenu where RoleId = {0})"
                    ,sqlWhere.AddParam(LoginUser.RoleId));
            }
            sqlWhere.AddSQLText(" order by MenuLevel,OrderNo");
            var menuList = DB.Query<TSysMenu>(sqlWhere).ToList(true);
            
            void LoadSubMenus(VM_HomeMenu pMenu)
            {
                foreach (var item in menuList)
                {
                    if (item.PId == pMenu.MenuId)
                    {
                        VM_HomeMenu m = new VM_HomeMenu();
                        m.MenuId = item.Id;
                        m.IsExpand = item.IsExpand;
                        m.MenuIcon = item.MenuIcon;
                        m.MenuName = item.MenuName;
                        m.MenuUrl = item.MenuUrl;
                        m.SubMenus = new List<VM_HomeMenu>();
                        LoadSubMenus(m);
                        pMenu.SubMenus.Add(m);
                    }
                }
            }
            List<VM_HomeMenu> rMenus = new List<VM_HomeMenu>();
            foreach (var item in menuList)
            {
                if (string.IsNullOrWhiteSpace(item.PId))
                {
                    VM_HomeMenu m = new VM_HomeMenu();
                    m.MenuId = item.Id;
                    m.IsExpand = item.IsExpand;
                    m.MenuIcon = item.MenuIcon;
                    m.MenuName = item.MenuName;
                    m.MenuUrl = item.MenuUrl;
                    m.SubMenus = new List<VM_HomeMenu>();
                    LoadSubMenus(m);
                    rMenus.Add(m);
                }
            }
            return rMenus;
        }
    }
}